import { login, logout, getInfo, register, getSmsCode, checkPhone, realNameAuth, realphone, setpassworld, realpassworld, changepin, createInviteCode, getInviteList } from '@/api/user'
import { getToken, setToken, removeToken } from '@/utils/auth'
import { resetRouter } from '@/router'

const getDefaultState = () => {
  return {
    // 把数据设置到这里
    token: getToken(), // token
    name: '', // 用户名
    avatar: '', // 头像
    accountStatus: null, // 用户账号信息
    accountInfo: null, // 用户账户信息（资产相关）
    roles:[], // 权限组
    invite_code:'' // 注册邀请码
  }
}

const state = getDefaultState()

const mutations = {
// 将从网页中获取的数据设置到这里，最后进行赋值到getDefaultState中
// 更改数据
  RESET_STATE: (state) => {
    Object.assign(state, getDefaultState())
  },
  SET_TOKEN: (state, token) => {
    state.token = token
  },
  SET_NAME: (state, name) => {
    state.name = name
  },
  SET_AVATAR: (state, avatar) => {
    state.avatar = avatar
  },
  SET_ROLES: (state, data) => {
    state.roles = data
  },
  SET_INVITECODE: (state, data) => {
    state.invite_code = data
  },
  // 保存用户的相关认证状态到state 
  SET_ACCOUNTSTATUS: (state, data) => {
    state.accountStatus = data
  },
  SET_ACCOUNTINFO: (state, data) => {
    state.accountInfo = data
  },
  // 实名认证
  SET_REALNAME: (state, data) => {
    // 更改实名认证的状态
    state.accountStatus.realNameAuth = 1
  },
  // 更改手机号
  SET_REALPHONE: (state, data) => {
    console.log('更换手机号的数据：',data)
    state.accountStatus.phoneStatus = 1
  },

  //  设置支付密码
  SET_SETPASSWORLD: (state, data) => {
    console.log('设置支付密码的data：',data)
    state.accountStatus.payPwdStatus = 1
  },

  //  修改登录密码
  SET_PASSWORLD: (state, data) => {
    console.log('修改登录密码的data：',data)
  },

  //  设置支付密码
  SET_CHANGE_PIN: (state, data) => {
    console.log('更换支付密码的data：',data)
  },
}

const actions = {
  // user login
  login({ commit }, userInfo) {
    const { username, password } = userInfo
    return new Promise((resolve, reject) => {
      login({ username: username.trim(), password: password }).then(response => {
        const { data } = response
        console.log('1',response)
        commit('SET_TOKEN', data.token)
        setToken(data.token)
        resolve()
      }).catch(error => {
        reject(error)
      })
    })
  },

  // get user info
  getInfo({ commit, state }) {
    return new Promise((resolve, reject) => {
      getInfo(state.token).then(response => {
        const { data } = response

        if (!data) {
          return reject('Verification failed, please Login again.')
        }

        const { name, roles, userInfoDate, accountInfo } = data
        console.log('获取的用户信息是',data,) // data是从response拿出来的
        // console.log('获取到的头像是',userInfoDate.avatar)


        // 这些是调用接口，在网页中获取数据,提交到上面的mutations中去
        commit('SET_NAME', name) // 用户名
        const avatar = userInfoDate.avatar || 'baseAvatar.jpg' // 如果他没有值或者为空的话，就给他一个默认的头像
        commit('SET_AVATAR', userInfoDate.avatar) // 头像
        commit('SET_INVITECODE', userInfoDate.invite_code) // 邀请码
        commit('SET_ROLES', roles) // 权限组
        commit('SET_ACCOUNTSTATUS', userInfoDate) // 用户账号信息
        commit('SET_ACCOUNTINFO', accountInfo) // 用户账户信息（资产相关）
        resolve(data)
      }).catch(error => {
        reject(error)
      })
    })
  },

  // user logout
  logout({ commit, state }) {
    return new Promise((resolve, reject) => {
      logout(state.token).then(() => {
        removeToken() // must remove  token  first
        resetRouter()
        commit('RESET_STATE')
        resolve()
      }).catch(error => {
        reject(error)
      })
    })
  },

  // 注册
  register({ commit, state }, data) {
    return new Promise((resolve, reject) => {
      register(data).then((res) => {
        const { data } = res
        resolve(data)
      }).catch(error => {
        reject(error)
      })
    })
  },
  

  // 获取验证码
  getSmsCode({ commit, state },phoneNumber) {
    return new Promise((resolve, reject) => {
      getSmsCode(phoneNumber).then((res) => {
        const{data} = res
        resolve(data)
      }).catch(error => {
        reject(error)
      })
    })
  },

  // 判断手机号是否重复
  checkPhone({ commit, state },phoneNumber) {
    return new Promise((resolve, reject) => {
      checkPhone(phoneNumber).then((res) => {
        const{data} = res
        resolve(data)
      }).catch(error => {
        reject(error)
      })
    })
  },

  // 实名认证
  realNameAuth({ commit, state },data) {
    return new Promise((resolve, reject) => {
      realNameAuth(data).then((res) => {
        commit('SET_REALNAME')
        resolve(data)
      }).catch(error => {
        reject(error)
      })
    })
  },

  // 更换手机号
  realphone({ commit, state },data) {
    console.log(data)
    return new Promise((resolve, reject) => {
      realphone(data).then((res) => {
        commit('SET_REALPHONE')
        resolve(data)
      }).catch(error => {
        reject(error)
      })
    })
  },

  // 修改登录密码
  realpassworld({ commit, state },data) {
    console.log(data)
    return new Promise((resolve, reject) => {
      realpassworld(data).then((res) => {
        commit('SET_PASSWORLD')
        resolve(data)
      }).catch(error => {
        reject(error)
      })
    })
  },

  // 设置支付密码
  setpassworld({ commit, state },data) {
    console.log(data)
    return new Promise((resolve, reject) => {
      setpassworld(data).then((res) => {
        commit('SET_SETPASSWORLD')
        resolve(data)
      }).catch(error => {
        reject(error)
      })
    })
  },

  // 更换支付密码
  changepin({ commit, state },data) {
    console.log(data)
    return new Promise((resolve, reject) => {
      changepin(data).then((res) => {
        commit('SET_CHANGE_PIN')
        resolve(data)
      }).catch(error => {
        reject(error)
      })
    })
  },

  // remove token
  resetToken({ commit }) {
    return new Promise(resolve => {
      removeToken() // must remove  token  first
      commit('RESET_STATE')
      resolve()
    })
  },

  // 生成邀请码
  createInviteCode({ commit, state },data) {
    return new Promise((resolve, reject) => {
      createInviteCode(data).then((res) => {
        // 获取到邀请码之后要保存到set里面去
        commit('SET_INVITECODE',res.data.invite_code)
        resolve(res.data)
      }).catch(error => {
        reject(error)
      })
    })
  },

  // 获取邀请列表
  getInviteList({ commit, state },data) {
    return new Promise((resolve, reject) => {
      getInviteList(data).then((res) => {
        resolve(res.data)
      }).catch(error => {
        reject(error)
      })
    })
  },
}

export default {
  namespaced: true,
  state,
  mutations,
  actions
}

